package com.sq.universe.flowable.controller;

import com.alibaba.fastjson.JSONObject;
import com.sq.universe.base.controller.BaseController;
import com.sq.universe.base.entity.RespEntity;
import com.sq.universe.flowable.dao.WorkflowDao;
import com.sq.universe.flowable.entity.WorkflowEntity;
import com.sq.universe.flowable.service.WorkflowService;
import com.sq.universe.system.entity.UserEntity;
import org.flowable.bpmn.model.FlowElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/workflow")
public class WorkflowController extends BaseController {

    @Autowired
    private WorkflowService workflowService;

    @Autowired
    private WorkflowDao workflowDao;


    @GetMapping("/test")
    public RespEntity test(@RequestBody(required = false) WorkflowEntity workflowEntity){
        return new RespEntity("查询成功",workflowDao.mapperTest());
    }



    @PostMapping("/queryTodo")
    public RespEntity queryTodo(@RequestBody(required = false) WorkflowEntity workflowEntity){
        return new RespEntity("查询成功",workflowService.queryTodo(this.getCurrentUser(),workflowEntity));
    }

    /**
     *
     * @param workflowEntity
     * @return
     */
    @PostMapping("/queryAllTodo")
    public RespEntity queryAllTodo(@RequestBody(required = false) WorkflowEntity workflowEntity){
        return new RespEntity("查询成功",workflowService.queryAllTodo(workflowEntity));
    }

    @PostMapping("/queryDone")
    public RespEntity queryDone(@RequestBody(required = false) WorkflowEntity workflowEntity){
        return new RespEntity("查询成功",workflowService.queryDone(this.getCurrentUser(),workflowEntity));
    }

    @PostMapping("/start")
    public RespEntity start(@RequestBody(required = false) WorkflowEntity workflowEntity){
        workflowService.start( workflowEntity, this.getCurrentUser());
        return new RespEntity("启动成功" );
    }

    @PostMapping("/completeTask")
    public RespEntity completeTask(@RequestBody(required = false) WorkflowEntity workflowEntity){
        workflowService.completeTask( workflowEntity, this.getCurrentUser());
        return new RespEntity("任务完成" );
    }

    @PostMapping("/rollback")
    public RespEntity rollback(@RequestBody(required = false) WorkflowEntity workflowEntity){
        workflowService.rollback( workflowEntity,this.getCurrentUser());
        return new RespEntity("退货");
    }

    @PostMapping("/getForm")
    public RespEntity getForm(String formkey){
        return new RespEntity("查询成功", workflowService.getForm(formkey) );
    }

    @PostMapping("/history")
    public RespEntity history(@RequestBody(required = false) WorkflowEntity workflowEntity){
        return new RespEntity("查询成功", workflowService.history(workflowEntity) );
    }

    /**
     * 查询当前任务，所在流程中所有的任务节点ID
     * @param taskId
     * @return
     */
    @PostMapping("/queryFlowNodes/{taskId}")
    public RespEntity  queryFlowNodes(@PathVariable("taskId") String taskId){
        return RespEntity.success("查询成功",workflowService.queryFlowNodes(taskId));
    }

    /**
     * 根据流程实例Id获取任务
     * @param processInstanceId
     * @return
     */
    @PostMapping("/queryTaskId")
    public RespEntity queryTaskId(String processInstanceId){
        return new RespEntity("查询成功", workflowService.queryTaskId(processInstanceId) );
    }

    /**
     * 根据流程实例Id获取任务
     * @param jsonData
     * @return
     */
    @PostMapping("/jump")
    public RespEntity jump(@RequestBody JSONObject jsonData){
        workflowService.jump(jsonData,this.getCurrentUser());
        return new RespEntity("查询成功");
    }

    /**
     * 任务转办
     * @param data
     * @return
     */
    @PostMapping("/transferTask")
    public RespEntity transferTask(@RequestBody JSONObject data){
        return workflowService.transferTask(data,this.getCurrentUser());
    }

    /**
     * 激活流程
     * @param data
     * @return
     */
    @PostMapping("/active")
    public RespEntity active(@RequestBody JSONObject data){
        return workflowService.active(data,this.getCurrentUser());
    }

    /**
     * 挂起流程
     * @param data
     * @return
     */
    @PostMapping("/suspend")
    public RespEntity suspend(@RequestBody JSONObject data){
        return workflowService.suspend(data,this.getCurrentUser());
    }

    /**
     * 查詢模型列表
     * @param data
     * @return
     */
    @PostMapping("/queryModelList")
    public RespEntity queryModelList(@RequestBody JSONObject data){
        List modelList = workflowService.queryModelList(data);
        return RespEntity.success("查询成功",modelList);
    }

    /**
     * 任务数据统计
     * @param data
     * @return
     */
    @PostMapping("/taskCount")
    public RespEntity taskCount(@RequestBody JSONObject data){
        return RespEntity.success("查询成功", workflowService.taskCount(this.getCurrentUser()));
    }
    /**
     * 统计各个流程的数据
     * @param data
     * @return
     */
    @PostMapping("/groupFlowByKey")
    public RespEntity groupFlowByKey(@RequestBody JSONObject data){
        return RespEntity.success("查询成功", workflowService.groupFlowByKey());
    }

}
